/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mbeans;

import ejb.CarrelloLocal;
import ejb.exceptions.ClienteNonPresenteException;
import ejb.exceptions.ProdottoNonDisponibileException;
import ejb.exceptions.ProdottoNonPresenteException;
import ejb.exceptions.ProdottoPresenteException;
import ejb.facade.ClienteFacadeLocal;
import entities.Cliente;
import entities.DettaglioOrdine;
import entities.Prodotto;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author sflesca
 */
@ManagedBean
@SessionScoped
public class Carrello {

    @EJB
    private ClienteFacadeLocal clienteFacade;

    @EJB
    private CarrelloLocal carrello;

    /**
     * Get the value of itemCount
     *
     * @return the value of itemCount
     */
    public int getItemCount() {
        return carrello.itemCount();
    }

    /**
     * Get the value of items
     *
     * @return the value of items
     */
    public Collection<DettaglioOrdine> getItems() {
        return carrello.contenuto();
    }

    public String add(Prodotto p) {
        try {
            carrello.add(p);
        } catch (ProdottoPresenteException ex) {
            return null;
//Logger.getLogger(Carrello.class.getName()).log(Level.INFO, null, ex);
        }
        return "VisualizzaCarrello";
    }

    public String ordina() {
        try {
            Cliente c = clienteFacade.find(1L);
            carrello.ordina(c);
        } catch (ClienteNonPresenteException ex) {
            Logger.getLogger(Carrello.class.getName()).log(Level.INFO, null, ex);
            return null;
        } catch (ProdottoNonDisponibileException ex) {
            Logger.getLogger(Carrello.class.getName()).log(Level.INFO, null, ex);
            //TODO inserire messaggio di errore
            return null;
        }
        return "Home";
    }

    public String svuota() {
        carrello.svuota();
        return null;
    }

    public String setQta(Prodotto p, int qta) {
        try {
            carrello.setQta(p, qta);
        } catch (ProdottoNonPresenteException ex) {
            Logger.getLogger(Carrello.class.getName()).log(Level.INFO, null, ex);
        } finally {
            return null;
        }
    }

    public String remove(Prodotto p) {
        try {
            carrello.remove(p);
        } catch (ProdottoNonPresenteException ex) {
            Logger
            .getLogger(Carrello.class.getName()).log(Level.INFO, null, ex);
        } finally {
            return null;
        }
    }

    /**
     * Creates a new instance of Carrello
     */
    public Carrello() {
    }

}
